﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="RepairingTicketManagement.aspx.cs" Inherits="WebApp.RepairingTickets.RepairingTicketManagement" %>

<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<asp:Content ID="Content1" ContentPlaceHolderID="scriptCssContent" runat="server">
    <script type="text/javascript">
        //Global variables
        var chkSelectOneClientID = "chkSelectOne";
        var chkSelectAllClientID = "chkSelectAll";
        var attributeName = "RepairingTicketHistoryID";
        var separate = "@";
        var hdRepairingTicketHistoryIDsClientID = "<%= hdRepairingTicketHistoryIDs.ClientID %>";

        //Event Handlers
        $(document).ready(function () {
            SetChecked(chkSelectAllClientID, chkSelectOneClientID, hdRepairingTicketHistoryIDsClientID);

            $("#<%= btnPrint.ClientID %>").click(function () {
                try {
                    var ids = GetValue(hdRepairingTicketHistoryIDsClientID);
                    if (jQuery.trim(ids) != "") {
                        var url = '<%= this.ResolveUrl("~/RepairingTickets/RepairingTicketList.aspx") %>' + "?IDs=" + ids;
                        //var url = '<%= this.ResolveUrl("~/Reports/ShowReport.aspx") %>' + "?ReportType=RepairingTicketList&IDs=" + ids;
                        openWindow(url, "", "800px", "600px");
                        return false;
                    }
                    else {
                        alert("Vui lòng chọn phiếu cần in phiếu đi sửa");
                        return false;
                    }

                } catch (e) {
                    alert(e);
                    return false;
                }
            });

            $("input[id*=" + chkSelectAllClientID + "]").change(function () {
                SelectAll(chkSelectAllClientID, chkSelectOneClientID, hdRepairingTicketHistoryIDsClientID, attributeName);
            })

            $("input[id*=" + chkSelectOneClientID + "]").change(function () {
                SelectOne(chkSelectAllClientID, chkSelectOneClientID);

                var id = $(this).parent().attr(attributeName);
                if (this.checked) {
                    AddValue(hdRepairingTicketHistoryIDsClientID, id);
                }
                else {
                    RemoveValue(hdRepairingTicketHistoryIDsClientID, id);
                }
            })
        });

        //Methods
        function SelectAll(chkSelectAllClientID, chkSelectOneClientID, hdRepairingTicketHistoryIDsClientID, attributeName) {
            try {
                var chkSelectAll = $("input[id*=" + chkSelectAllClientID + "]");
                if (chkSelectAll != null) {
                    
                    $("input[id*=" + chkSelectOneClientID + "]").each(function () {
                        this.checked = chkSelectAll[0].checked;

                        var id = $(this).parent().attr(attributeName);

                        //Remove old value from ids string
                        RemoveValue(hdRepairingTicketHistoryIDsClientID, id);

                        //Add or remove new value to ids string
                        if (chkSelectAll[0].checked) {
                            AddValue(hdRepairingTicketHistoryIDsClientID, id);
                        }
                        else {
                            RemoveValue(hdRepairingTicketHistoryIDsClientID, id);
                        }
                    })
                }
            } catch (e) {
                alert(e);
            }
        }

        function SelectOne(chkSelectAllClientID, chkSelectOneClientID) {
            try {
                var chkSelectAll = $("input[id*=" + chkSelectAllClientID + "]");
                var isSelectAll = true;

                if (chkSelectAll != null) {
                    $("input[id*=" + chkSelectOneClientID + "]").each(function () {
                        if (!this.checked) {
                            isSelectAll = false;
                        }
                    })

                    if (isSelectAll) {
                        chkSelectAll[0].checked = true;
                    }
                    else {
                        chkSelectAll[0].checked = false;
                    }
                }
            } catch (e) {
                alert(e);
            }
        }

        function SetChecked(chkSelectAllClientID, chkSelectOneClientID, hdRepairingTicketHistoryIDsClientID) {
            try {
                var ids = GetValue(hdRepairingTicketHistoryIDsClientID);
                if(ids != "")
                {
                    var arrID = ids.split(separate);
                    if (arrID != null) {
                        //set checkbox select one
                        $("input[id*=" + chkSelectOneClientID + "]").each(function () {
                            var id = $(this).parent().attr(attributeName);
                            for (var i = 0; i < arrID.length; i++) {
                                if (id == arrID[i]) {
                                    this.checked = true;
                                }
                            }
                        })

                        //set checkbox selet all
                        SelectOne(chkSelectAllClientID, chkSelectOneClientID);
                    }
                }
            } catch (e) {
                alert(e);
            }
        }

        function AddValue(hdRepairingTicketHistoryIDsClientID, value) {
            try {
                var hdRepairingTicketHistoryIDs = $("#" + hdRepairingTicketHistoryIDsClientID);
                if (hdRepairingTicketHistoryIDs != null) {
                    hdRepairingTicketHistoryIDs.val(hdRepairingTicketHistoryIDs.val() + value + separate);
                }
            } catch (e) {
                alert(e);
            }
        }

        function RemoveValue(hdRepairingTicketHistoryIDsClientID, value) {
            try {
                var hdRepairingTicketHistoryIDs = $("#" + hdRepairingTicketHistoryIDsClientID);
                if (hdRepairingTicketHistoryIDs != null) {
                    
                    //If value isn't first element in ids string
                    if (hdRepairingTicketHistoryIDs.val().indexOf(value) != -1 && hdRepairingTicketHistoryIDs.val().indexOf(value) != 0) {
                        hdRepairingTicketHistoryIDs.val(hdRepairingTicketHistoryIDs.val().replace(separate + value + separate, separate));
                    }
                    else {
                        //if value is first element in ids string
                        if (hdRepairingTicketHistoryIDs.val().indexOf(value) == 0) {
                            hdRepairingTicketHistoryIDs.val(hdRepairingTicketHistoryIDs.val().substring(value.toString().length + separate.length));   
                        }
                    }
                    
                }
            } catch (e) {
                alert(e);
            }
        }

        function GetValue(hdRepairingTicketHistoryIDsClientID) {
            var ids = "";
            try {
                var hdRepairingTicketHistoryIDs = $("#" + hdRepairingTicketHistoryIDsClientID);
                if (hdRepairingTicketHistoryIDs != null) {
                    if (jQuery.trim(hdRepairingTicketHistoryIDs.val()) != "") {
                        ids = hdRepairingTicketHistoryIDs.val().substr(0, hdRepairingTicketHistoryIDs.val().length - separate.length);
                    }
                }
            } catch (e) {
                ids = ""
            }
            return ids;
        }
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="titlePlaceHolder" runat="server">
    <asp:literal id="pageTitle" runat="server"></asp:literal>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="menuPlaceHolder" runat="server">
    <one:TabStrip ID="tspTicketManagement" runat="server" selectedtabcssclass="Current">
        <one:Tab name="receivingTicket" NavigateUrl="~/ReceivingTicket/ReceivingTicketEdit.aspx" Text="<%$ Resources:Strings, ReceivingTicket%>"></one:Tab>
        <one:Tab Name="repairingTicket" NavigateUrl="~/RepairingTickets/RepairingTicketManagement.aspx" Text="<%$ Resources:Strings, RepairingTicket%>"></one:Tab>
        <one:Tab Name="checkWarranty" NavigateUrl="~/RepairingTickets/CheckWarrantyManagement.aspx" Text="<%$ Resources:Strings, CheckWarranty%>" AllowRoles="Administrators,WarrantyConfirmers"></one:Tab>
        <one:Tab Name="verifyRepairQuality" NavigateUrl="~/RepairingTickets/VerifyRepairQualityManagement.aspx" Text="<%$ Resources:Strings, VerifyRepairQuality%>" AllowRoles="Administrators,RepairQualityConfirmers"></one:Tab>
        <one:Tab Name="searchRepairInfomation" NavigateUrl="~/ReceivingTicket/ReceivingTicketManagement.aspx" Text="<%$ Resources:Strings, SearchRepairInfomation%>"></one:Tab>
    </one:TabStrip>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="contentPlaceHolder" runat="server">
    <fieldset title="Thông tin tìm kiếm">
        <legend>Thông tin tìm kiếm</legend>
            <table style="width:100%; border:0px;">
                <tr>
                    <td class="ISoft_LabelTitle" style="width:15%;">Loại sản phẩm</td>
                    <td class="ISoft_Input" style="width:35%;">
                        <telerik:RadComboBox
                            ID="rcbProductType" Runat="server" Skin="<%$ Resources:Strings, Skin%>" 
                            MarkFirstMatch="True" Width="100%" Height="150px" 
                            AllowCustomText="True" EmptyMessage="Chọn loại sản phẩm">
                            <ExpandAnimation Type="None" />
                            <CollapseAnimation Type="None" />
                        </telerik:RadComboBox>
                    </td>

                    <td class="ISoft_LabelTitle">Tình trạng</td>
                    <td class="ISoft_Input">
                        <telerik:RadComboBox 
                            ID="rcbStatus" Runat="server" Skin="<%$ Resources:Strings, Skin%>"
                            MarkFirstMatch="True" Width="100%" Height="150px" 
                            AllowCustomText="True" EmptyMessage="Chọn tình trạng">
                            <ExpandAnimation Type="None" />
                            <CollapseAnimation Type="None" />
                        </telerik:RadComboBox>
                    </td>
                </tr>

                <tr>
                    <td class="ISoft_LabelTitle" style="width:15%;">Ngày nhận từ</td>
                    <td class="ISoft_Input" style="width:35%;">
                        <telerik:RadDatePicker ID="rdpCreatedDateFrom" Runat="server" Skin="<%$ Resources:Strings, Skin%>" 
                                                Culture="Vietnamese (Vietnam)" Width="150px">
                            <Calendar ID="Calendar2" runat="server" UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" 
                                        ViewSelectorText="x" Skin="<%$ Resources:Strings, Skin%>" >
                            </Calendar>
                            <DatePopupButton ImageUrl="" HoverImageUrl="" ToolTip="Bấm vào đây để chọn ngày."></DatePopupButton>
                            <DateInput ID="DateInput2" runat="server" DisplayDateFormat="dd/MM/yyyy" DateFormat="dd/MM/yyyy" EmptyMessage="Chọn ngày tiếp nhận">
                                <ClientEvents OnError="DateError" />
                            </DateInput>
                            <ClientEvents OnDateSelected="DateSelected" />
                        </telerik:RadDatePicker>
                    </td>

                    <td class="ISoft_LabelTitle" style="width:15%;">đến</td>
                    <td class="ISoft_Input" style="width:35%;">
                        <telerik:RadDatePicker ID="rdpCreatedDateTo" Runat="server" Skin="<%$ Resources:Strings, Skin%>" 
                                                Culture="Vietnamese (Vietnam)" Width="150px">
                            <Calendar ID="Calendar1" runat="server" UseRowHeadersAsSelectors="False" UseColumnHeadersAsSelectors="False" 
                                        ViewSelectorText="x" Skin="<%$ Resources:Strings, Skin%>" >
                            </Calendar>

                            <DatePopupButton ImageUrl="" HoverImageUrl="" ToolTip="Bấm vào đây để chọn ngày."></DatePopupButton>
                            <DateInput ID="DateInput1" runat="server" DisplayDateFormat="dd/MM/yyyy" DateFormat="dd/MM/yyyy" EmptyMessage="Chọn ngày tiếp nhận">
                                <ClientEvents OnError="DateError" />
                            </DateInput>
                            <ClientEvents OnDateSelected="DateSelected" />
                        </telerik:RadDatePicker>
                    </td>
                </tr>
            </table>
    </fieldset>

    <table style="width:100%; border:0px;">
        <tr>
            <td class="ISoft_ButtonsRow">
                <telerik:RadButton ID="btnSearch" runat="server" 
                    Skin="<%$ Resources:Strings, Skin%>" Text="<%$ Resources:Strings, Search%>" 
                    Width="100px" onclick="btnSearch_Click"></telerik:RadButton>
                <telerik:RadButton ID="btnPrint" runat="server" 
                    Skin="<%$ Resources:Strings, Skin%>" Text="<%$ Resources:Strings, Print%>" 
                    Width="100px"></telerik:RadButton>
            </td>
        </tr>

        <tr>
            <td>
                <telerik:RadGrid ID="grdResult" runat="server" AllowPaging="true" AllowSorting="true"
                                EnableEmbeddedSkins="true" 
                    Skin="<%$ Resources:Strings, Skin%>" 
                    onitemcreated="grdResult_ItemCreated" 
                    onneeddatasource="grdResult_NeedDataSource" 
                    onitemcommand="grdResult_ItemCommand" 
                    onitemdatabound="grdResult_ItemDataBound" 
                    onpageindexchanged="grdResult_PageIndexChanged" 
                    onpagesizechanged="grdResult_PageSizeChanged">
                    <HeaderStyle Wrap="true" Font-Bold="true" HorizontalAlign="Center" Font-Size="10pt" />
                    <AlternatingItemStyle Font-Names="arial,tahoma,sans-serif" Font-Size="12px" BorderStyle="Solid"/>                
                    <ItemStyle Font-Names="arial,tahoma,sans-serif" Font-Size="12px" BorderColor="#2299DD"/>
                    
                    <PagerStyle  HorizontalAlign="Right" Mode="NextPrevNumericAndAdvanced"
                                PagerTextFormat="Chọn trang: {4} &amp;nbsp;|&amp;nbsp; Trang hiện tại: {0} / {1}, mẫu tin: {2} - {3} / {5}." AlwaysVisible="True"/>
                    
                    <MasterTableView AutoGenerateColumns="false" NoMasterRecordsText="<%$ Resources:Strings, NoMasterRecordsText%>">
            
                        <Columns>
                            <telerik:GridTemplateColumn HeaderText="STT">
                                <HeaderStyle Width="30px" />
                                <ItemStyle HorizontalAlign="Center" />
                                <ItemTemplate>
                                    <asp:Label style="text-align:center" id="lblSTT" runat="server" Text='<%# grdResult.CurrentPageIndex*grdResult.PageSize + grdResult.Items.Count+1 %>' />
                                </ItemTemplate>
                            </telerik:GridTemplateColumn>
                
                            <telerik:GridBoundColumn DataField="RepairingTicketHistoryID" UniqueName="RepairingTicketHistoryID" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="StatusID" UniqueName="StatusID" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="DisplayColor" UniqueName="DisplayColor" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="IsStart" UniqueName="IsStart" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="AllowEdit" UniqueName="AllowEdit" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="AllowRepair" UniqueName="AllowRepair" Visible="false">
                            </telerik:GridBoundColumn>
                            
                            <telerik:GridBoundColumn DataField="AllowRepairNoComplete" UniqueName="AllowRepairNoComplete" Visible="false">
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn DataField="AllowRepairComplete" UniqueName="AllowRepairComplete" Visible="false">
                            </telerik:GridBoundColumn>
            
                            <telerik:GridBoundColumn HeaderText="Tên KH" DataField="FullName" UniqueName="FullName" SortExpression="FullName">
                                <ItemStyle HorizontalAlign="Left" Width="10%"/>
                            </telerik:GridBoundColumn>
                
                            <telerik:GridBoundColumn HeaderText="SĐT" DataField="Mobile" UniqueName="Mobile" SortExpression="Mobile">
                                <ItemStyle HorizontalAlign="Left" Width="10%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Địa Chỉ" DataField="Address" UniqueName="Address" SortExpression="Address">
                                <ItemStyle HorizontalAlign="Left" Width="13%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Loại Sản Phẩm" DataField="ProductTypeName" UniqueName="ProductTypeName" SortExpression="ProductTypeName">
                                <ItemStyle HorizontalAlign="Left" Width="10%"/>
                            </telerik:GridBoundColumn>
                
                            <telerik:GridBoundColumn HeaderText="Model" DataField="Model" UniqueName="Model" SortExpression="Model">
                                <ItemStyle HorizontalAlign="Left" Width="9%"/>
                            </telerik:GridBoundColumn>
                            
                            <telerik:GridBoundColumn HeaderText="Serial" DataField="SerialNumber" UniqueName="SerialNumber" SortExpression="SerialNumber">
                                <ItemStyle HorizontalAlign="Left" Width="9%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Ngày Nhận" DataField="CreatedDate" UniqueName="CreatedDate" SortExpression="CreatedDate">
                                <ItemStyle HorizontalAlign="Center" Width="10%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Tình Trạng" DataField="StatusName" UniqueName="StatusName" SortExpression="StatusName">
                                <ItemStyle HorizontalAlign="Left" Width="10%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Người Sửa" DataField="Repairer" UniqueName="Repairer" SortExpression="Repairer">
                                <ItemStyle HorizontalAlign="Left"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridBoundColumn HeaderText="Lý Do" DataField="Description" UniqueName="Description" SortExpression="Description">
                                <ItemStyle HorizontalAlign="Left" Width="10%"/>
                            </telerik:GridBoundColumn>

                            <telerik:GridTemplateColumn HeaderText="Sửa" UniqueName="ColEdit">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imbEdit" runat="server" ImageUrl="~/images/Edit.gif" ImageAlign="Middle" CommandName="cmdEdit"/>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="30px" />
                            </telerik:GridTemplateColumn>

                            <telerik:GridTemplateColumn HeaderText="Nhận Sửa" UniqueName="ColRepair">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imbRepair" runat="server" ImageUrl="~/images/repair.gif" ImageAlign="Middle" CommandName="cmdRepair"/>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="30px" />
                            </telerik:GridTemplateColumn>

                            <telerik:GridTemplateColumn HeaderText="Sửa Chưa Xong" UniqueName="ColRepairNoComplete">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imbRepairUnComplete" runat="server" ImageUrl="~/images/warning.gif" ImageAlign="Middle" CommandName="cmdRepairUnComplete"/>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="30px" />
                            </telerik:GridTemplateColumn>

                            <telerik:GridTemplateColumn HeaderText="Sửa Xong" UniqueName="ColRepairCompleted">
                                <ItemTemplate>
                                    <asp:ImageButton ID="imbRepairComplete" runat="server" ImageUrl="~/images/completed.gif" ImageAlign="Middle" CommandName="cmdRepairCompleted"/>
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="30px" />
                            </telerik:GridTemplateColumn>

                            <telerik:GridTemplateColumn UniqueName="ColSelect">
                                <HeaderTemplate>
                                    <asp:CheckBox runat="server" ID="chkSelectAll" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox runat="server" ID="chkSelectOne" RepairingTicketHistoryID='<%# DataBinder.Eval(Container.DataItem, "RepairingTicketHistoryID") %>' />
                                </ItemTemplate>
                                <ItemStyle HorizontalAlign="Center" Width="20px" />
                            </telerik:GridTemplateColumn>
            
                        </Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </td>
        </tr>
    </table>

    <input runat="server" type="hidden" id="hdRepairingTicketHistoryIDs" value=""/>
</asp:Content>
